package edu.cs.cs160.storytime;

import java.util.ArrayList;

public class StoryLibrary {
	private ArrayList<BookData> books;
	private int bookCount;
	private int selectedBook;
	private StoryPage blankPage;
	
	public StoryLibrary() {
		this.bookCount = 0;
		this.books = new ArrayList<BookData>();
		this.blankPage = new StoryPage(R.drawable.blankpage);
	}
	public class BookData {
		private StoryBook book;
		private boolean enabled;
		
		public BookData(StoryBook book,boolean enabled){
			this.book = book;
			this.enabled = enabled;
		}
		public StoryBook getBook() {
			return this.book;
		}
		public boolean getEnabled() {
			return this.enabled;
		}
		public void setEnabled(boolean b) {
			this.enabled = b;
		}
	}
	
	public StoryBook getBook(int bookID) {
		return books.get(bookID).getBook();	
	}
	
	public int getBookCount() {
		return this.bookCount;	
	}	
	
	public boolean isBookEnabled(int bookID) {
		return books.get(bookID).getEnabled();
	}
	
	public void selectBook(int bookID) {
		this.selectedBook = bookID;
	}
	
	public int getSelectedID() {
		return this.selectedBook;
	}
	
	public void addBook(StoryBook book,boolean enabled) {
		BookData bd = new BookData(book,enabled);
		
		this.books.add(bd);
		book.setID(this.bookCount);
		this.bookCount++;
	}
	
	public void enableBook(StoryBook book) {
		books.get(this.books.indexOf(book)).setEnabled(true);
	}
	public void disableBook(StoryBook book) {
		books.get(this.books.indexOf(book)).setEnabled(false);
	}
	
	public StoryPage getBlankPage() {
		return blankPage;
	}
	
	public void SortByTitle()
	{
		for(int i = 1; i < this.books.size(); i++)
		{
			for(int j = 0; j < i; j++)
			{
				if(this.books.get(i).getBook().getName().compareTo(this.books.get(j).getBook().getName()) < 0)
				{
					BookData temp = this.books.get(i);
					for(int k = (i - j); k > 0; k--)
					{
						this.books.set(j + k, this.books.get(j + k - 1));
					}
					this.books.set(j, temp);
					break;
				}
			}
		}
	}
	public void SortByAuthor()
	{
		for(int i = 1; i < this.books.size(); i++)
		{
			for(int j = 0; j < i; j++)
			{
				if(this.books.get(i).getBook().getAuthor().compareTo(this.books.get(j).getBook().getAuthor()) < 0)
				{
					BookData temp = this.books.get(i);
					for(int k = (i - j); k > 0; k--)
					{
						this.books.set(j + k, this.books.get(j + k - 1));
					}
					this.books.set(j, temp);
					break;
				}
			}
		}
	}
}
